package com.demo.core.controller;

import com.demo.base.page.IPagableDTO;
import com.demo.base.page.PagableDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.base.model.PublicNotice;
import com.demo.base.page.IPagableDTO;
import com.demo.base.page.PagableDTO;
import com.demo.base.util.CommonUtil;
import com.demo.core.service.PublicMessageService;

/**
 * Created by jiandandia on 2017/4/5.
 */
@RequestMapping("/PublicMessage")
@Controller
public class PublicMessageController{
    @Autowired

    private PublicMessageService publicMessageService;


    /**
     * //新增或修改公告信息
     * @param publicNotice
     * @return
     */
    @ResponseBody
    @RequestMapping("/saveOrUpdatePublicNotice")
    public String saveOrUpdatePublicNotice(@RequestBody PublicNotice publicNotice){

        try {
            publicMessageService.saveOrUpdatePublicNotice(publicNotice);
        } catch (Exception e) {
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }

    /**
     * //浏览数，每次新增1
     * @param noticeId
     * @return
     */
    @ResponseBody
    @RequestMapping("/addBrowNumber")
    public String addBrowsingNumber(@RequestBody String noticeId){

        try {
            publicMessageService.addBrowsingNumber(noticeId);
        } catch (Exception e) {
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }

    /**
     * //关注数，每次新增1
     * @param noticeId
     * @return
     */
    @ResponseBody
    @RequestMapping("/addFollowNumber")
    public String addFollowersNumber(@RequestBody String noticeId){

        try {
            publicMessageService.addFollowersNumber(noticeId);
        } catch (Exception e) {
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }


    /**
     * //关闭当前公告信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/closePublicNotice")
    public String closePublicNotice(@RequestBody String id){
        try {
            publicMessageService.addFollowersNumber(id);
        } catch (Exception e) {
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }

    /**
     * 根据条件查询公告信息
     * @param queryVal
     * @param startRow
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryPublicNotices")
    public IPagableDTO<PublicNotice> queryPublicNotices(@RequestBody String queryVal, int startRow, int pageSize){

            IPagableDTO pagableDTO = new PagableDTO();
            pagableDTO.setStartRow(startRow);
            pagableDTO.setPageSize(pageSize);
           return publicMessageService.queryPublicNotices(queryVal, pagableDTO);

    }

    //删除单条或多条公告信息
    @ResponseBody
    @RequestMapping("/deltePublicNoticesById")
    public String deltePublicNoticesById(@RequestBody String ids){
        try{
            publicMessageService.deltePublicNoticesById(ids);
        }catch(Exception e){
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }
}
